package com.hongshan.wallet.core.trading

import android.app.Application
import android.databinding.ObservableField
import android.view.View
import com.hongshan.wallet.R
import com.hongshan.wallet.base.BaseOnPageChangeListener
import com.hongshan.wallet.base.MyViewModel
import com.hongshan.wallet.base.SingleLiveEvent
import com.hongshan.wallet.format

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.trading
 * @author   : zhangxx
 * @date     : 2019/3/22
 * @email    : qq957413212@hotmail.com
 */
class TradingViewModel(application: Application) : MyViewModel(application) {

    val pageIndex = ObservableField(0)

    val startTime = ObservableField<String>()
    val endTime = ObservableField<String>()

    val dateLong = ObservableField<Long>(System.currentTimeMillis()/1000)
    val timeLong = ObservableField<Long>(System.currentTimeMillis()/1000)

    val pickerStatus = ObservableField<Boolean>(false)
    val datePickerStatus = ObservableField<Boolean>(false)
    val timePickerStatus = ObservableField<Boolean>(false)

    var clickId = 0
    var completeStartTime:String? = "开始时间"
    var completeStartTimeLong:Long? = null
    var completeEndTime:String? = "结束时间"
    var completeEndTimeLong:Long? = null
    var staleStartTime:String? = "开始时间"
    var staleStartTimeLong:Long? = null
    var staleEndTime:String? = "结束时间"
    var staleEndTimeLong:Long? = null

    val refresh = SingleLiveEvent<Array<Long?>>()

    val pickerOK = View.OnClickListener {
        when (pageIndex.get()) {
            1 -> {
                val date = dateLong.get()?.format()
                if (clickId == R.id.tvStartTime) {
                    completeStartTimeLong = dateLong.get()
                    completeStartTime = date
                    startTime.set(date)
                } else {
                    completeEndTime = date
                    endTime.set(date)
                    completeEndTimeLong = dateLong.get()
                }
                refresh.value = arrayOf(completeStartTimeLong,completeEndTimeLong)
            }
            2 -> {
                val time = timeLong.get()?.format("HH:mm")
                if (clickId == R.id.tvStartTime) {
                    staleStartTimeLong = timeLong.get()
                    staleStartTime = time
                    startTime.set(time)
                } else {
                    staleEndTime = time
                    staleEndTimeLong = timeLong.get()
                    endTime.set(time)
                }
                refresh.value = arrayOf(staleStartTimeLong,staleEndTimeLong)
            }
        }
        pickerCancle.onClick(it)
    }

    val pickerCancle = View.OnClickListener {
        timePickerStatus.set(false)
        datePickerStatus.set(false)
        pickerStatus.set(false)
        clickId = 0
    }

    val showPicker = View.OnClickListener {
        clickId = it.id
        when (pageIndex.get()) {
            1 -> showTimePicker()
            2 -> showDatePicker()
        }
    }

    private fun showTimePicker() {
        timePickerStatus.set(true)
        datePickerStatus.set(false)
        pickerStatus.set(true)
    }

    private fun showDatePicker() {
        timePickerStatus.set(false)
        datePickerStatus.set(true)
        pickerStatus.set(true)
    }

    val onPagerChange = BaseOnPageChangeListener {
        pageIndex.set(it)
        pickerCancle.onClick(null)
        when(it){
            0 -> {
                refresh.value = arrayOf(null,null)
                startTime.set(null)
                endTime.set(null)
            }
            1 -> {
                startTime.set(completeStartTime)
                endTime.set(completeEndTime)
                refresh.value = arrayOf(completeStartTimeLong,completeEndTimeLong)
            }
            2 -> {
                startTime.set(staleStartTime)
                endTime.set(staleEndTime)
                refresh.value = arrayOf(staleStartTimeLong,staleEndTimeLong)
            }
        }
    }
}